package com.qf.service;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/21 11:18
 */

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 品牌制造商
 */
@Service
public class BrandServiceimpl implements BrandService{
    @Autowired
    private DtsBrandMapper dtsBrandMapper;
    @Override
    public IPage<DtsBrand> findAll(Integer page, Integer limit, String sort, String desc,Integer id, String name) {
        IPage<DtsBrand> page1=new Page<>(page,limit);
        QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);
        if(id!=null){
            queryWrapper.eq("id",id);
        }
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(sort)&& !StringUtils.isEmpty(desc)){
            if(desc.equalsIgnoreCase("desc")){
//                升序
                queryWrapper.orderByAsc(sort);
            }else{
//                降序
                queryWrapper.orderByDesc(sort);
            }
        }
        IPage<DtsBrand> brandIPage = dtsBrandMapper.selectPage(page1, queryWrapper);
        return brandIPage;
    }
    /**
     * 添加
     */
    @Override
    public  int  add(DtsBrand dtsBrand){
        dtsBrand.setDeleted(false);
        dtsBrand.setAddTime(new Date());
        dtsBrand.setUpdateTime(new Date());
        int insert = dtsBrandMapper.insert(dtsBrand);
        return insert;
    }

    /**
     * 删除
     * @param dtsBrand
     */
    @Override
    public void delete(DtsBrand dtsBrand) {
        dtsBrand.setDeleted(true);
        dtsBrandMapper.updateById(dtsBrand);

    }

    /**
     * 修改
     * @param dtsBrand
     */
    @Override
    public void update(DtsBrand dtsBrand) {
        dtsBrand.setUpdateTime(new Date());
        dtsBrandMapper.updateById(dtsBrand);
    }
}
